<?php
class Account_log_model extends CI_Model
{
    private $table   = 'account_log';
    private $table_1 = 'user';
    private $table_2 = 'admin_user';

    public function total($postData = array())
    {
        $this->db->from($this->table.' AS account_log');
        $this->db->join($this->table_1 . ' AS user', 'account_log.uid = user.uid');
        if (!empty($postData['username'])) {
            $this->db->where("((`user`.`uid` = '{$postData['username']}') OR (`user`.`user_name` LIKE '%{$postData['username']}%'))");
        }
        if (!empty($postData['product_type'])) {
            $this->db->where('account_log.product_type', $postData['product_type']);
        }
        if (!empty($postData['order_id'])) {
            $this->db->where('account_log.order_id', $postData['order_id']);
        }
        if (!empty($postData['admin_user'])) {
            $this->db->where('account_log.remittance_person', $postData['admin_user']);
        }
        if (!empty($postData['type'])) {
            $this->db->where('account_log.type', $postData['type']);
        }
        if (!empty($postData['account_type'])) {
            $this->db->where('account_log.account_type', $postData['account_type']);
        }
        if (!empty($postData['start_time'])) {
            $this->db->where('account_log.date >= ', $postData['start_time']. ' 00:00:01');
        }
        if (!empty($postData['end_time'])) {
            $this->db->where('account_log.date <= ', $postData['end_time']. ' 23:59:59');
        }
        return $this->db->count_all_results();
    }

    public function page_list($page_num, $num, $postData = array())
    {
        $this->db->select('account_log.*,user.user_name,user.alias_name,user.mobile_phone,user.user_type,user.parent_id');
        $this->db->from($this->table . ' AS account_log');
        $this->db->join($this->table_1 . ' AS user', 'account_log.uid = user.uid');
        if (!empty($postData['username'])) {
            $this->db->where("((`user`.`uid` = '{$postData['username']}') OR (`user`.`user_name` LIKE '%{$postData['username']}%'))");
        }
        if (!empty($postData['product_type'])) {
            $this->db->where('account_log.product_type', $postData['product_type']);
        }
        if (!empty($postData['order_id'])) {
            $this->db->where('account_log.order_id', $postData['order_id']);
        }
        if (!empty($postData['admin_user'])) {
            $this->db->where('account_log.remittance_person', $postData['admin_user']);
        }
        if (!empty($postData['type'])) {
            $this->db->where('account_log.type', $postData['type']);
        }
        if (!empty($postData['account_type'])) {
            $this->db->where('account_log.account_type', $postData['account_type']);
        }
        if (!empty($postData['start_time'])) {
            $this->db->where('account_log.date >= ', $postData['start_time']. ' 00:00:01');
        }
        if (!empty($postData['end_time'])) {
            $this->db->where('account_log.date <= ', $postData['end_time']. ' 23:59:59');
        }
        $this->db->order_by('account_log.log_id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }
    
    public function acoountLogTotal($postData = array())
    {
        $this->db->select_sum('amount');
        $this->db->from($this->table . ' AS account_log');
        $this->db->join($this->table_1 . ' AS user', 'account_log.uid = user.uid');
        if (!empty($postData['username'])) {
            $this->db->where("((`user`.`uid` = '{$postData['username']}') OR (`user`.`user_name` LIKE '%{$postData['username']}%'))");
        }
        if (!empty($postData['product_type'])) {
            $this->db->where('account_log.product_type', $postData['product_type']);
        }
        if (!empty($postData['order_id'])) {
            $this->db->where('account_log.order_id', $postData['order_id']);
        }
        if (!empty($postData['admin_user'])) {
            $this->db->where('account_log.remittance_person', $postData['admin_user']);
        }
        if (!empty($postData['type'])) {
            $this->db->where('account_log.type', $postData['type']);
        }
        if (!empty($postData['account_type'])) {
            $this->db->where('account_log.account_type', $postData['account_type']);
        }
        if (!empty($postData['start_time'])) {
            $this->db->where('account_log.date >= ', $postData['start_time']);
        }
        if (!empty($postData['end_time'])) {
            $this->db->where('account_log.date <= ', $postData['end_time']);
        }
        return $this->db->get();
    }

    public function insertAccountLog($postData)
    {
        $postData['date'] = date('Y-m-d H:i:s', time());

        $this->db->insert($this->table, $postData);
        return $this->db->insert_id();
    }

    public function accountLogUidTotal($data = array())
    {
        $this->db->select('log_id');
        $this->db->from($this->table . ' AS account_log');
        $this->db->join($this->table_1 . ' AS user', 'account_log.uid = user.uid', 'left');
        $this->db->join($this->table_2 . ' AS admin_user', 'account_log.remittance_person = admin_user.id', 'left');
        if ( ! empty($data['log_id'])) {
            $this->db->where('account_log.log_id', $data['log_id']);
        }
        if ( ! empty($data['minAmount'])) {
            $this->db->where('account_log.amount >=', $data['minAmount']);
        }
        if ( ! empty($data['maxAmount'])) {
            $this->db->where('account_log.amount <=', $data['maxAmount']);
        }
        if (isset($data['uid']) && $data['uid']) {
            $this->db->where('account_log.uid', $data['uid']);
        }
        if ( !empty($data['type_not_in'])) {
            $this->db->where_not_in('account_log.type', $data['type_not_in']); //过滤酒店
        }
        if (!empty($data['type'])) {
            $this->db->where_in('account_log.type', $data['type']); //过滤酒店
        }
        if ( ! empty($data['user_name'])) {
            $this->db->where('user.user_name', $data['user_name']); //被充值者名称
        }
        if (!empty($data['name'])) {
            $this->db->where('admin_user.name', $data['name']); //充值者名称
        }
        if (!empty($data['startDate'])) {
            $this->db->where('account_log.date >=', $data['startDate']. ' 00:00:00');
        }
        if (!empty($data['endDate'])) {
            $this->db->where('account_log.date <=', $data['endDate'] . ' 23:59:59');
        }

        return $this->db->count_all_results();
    }

    public function accountLogUidList($num, $data = array())
    {
        $this->db->select('account_log.*, user.user_name, user.alias_name, user.uid, admin_user.name, admin_user.realname');
        $this->db->from($this->table . ' as account_log');
        $this->db->join($this->table_1 . ' as user', 'account_log.uid = user.uid', 'left');
        $this->db->join($this->table_2 . ' as admin_user', 'account_log.remittance_person = admin_user.id', 'left');
        if ( ! empty($data['log_id'])) {
            $this->db->where('account_log.log_id', $data['log_id']);
        }
        if ( ! empty($data['minAmount'])) {
            $this->db->where('account_log.amount >=', $data['minAmount']);
        }
        if ( ! empty($data['maxAmount'])) {
            $this->db->where('account_log.amount <=', $data['maxAmount']);
        }
        if (isset($data['uid']) && $data['uid']) {
            $this->db->where('account_log.uid', $data['uid']);
        }
        if ( !empty($data['type_not_in'])) {
            $this->db->where_not_in('account_log.type', $data['type_not_in']); //过滤酒店
        }
        if (!empty($data['type'])) {
            $this->db->where_in('account_log.type', $data['type']); //过滤酒店
        }
        if ( ! empty($data['user_name'])) {
            $this->db->where('user.user_name', $data['user_name']); //被充值者名称
        }
        if (!empty($data['name'])) {
            $this->db->where('admin_user.name', $data['name']); //充值者名称
        }
        if (!empty($data['startDate'])) {
            $this->db->where('account_log.date >=', $data['startDate']. ' 00:00:00');
        }
        if (!empty($data['endDate'])) {
            $this->db->where('account_log.date <=', $data['endDate'] . ' 23:59:59');
        }

        $this->db->order_by('account_log.date', 'desc');
        $this->db->limit(20, $num);
        return $result = $this->db->get();
    }

    /**
     * 账户记录
     * @param unknown $orderProfitItem
     * @param unknown $amount_carry
     * @param unknown $type
     */
    public function insertAccountLogRecord($order_id, $uid, $amount, $amount_org, $type, $account_type = 1, $product_type = 'line')
    {
        $data = array(
            'order_id' => $order_id,
            'uid' => $uid,
            'amount' => $amount,
            'amount_carry' => $amount_org,
            'date' => date('Y-m-d H:i:s', time()),
            'type' => $type, //16代表后台虚拟充值
            'account_type' => $account_type,
            'product_type' => $product_type,
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    /*
     *生成返还退款信息
     * */
    public function insertAccountLogTransport($order_id, $uid, $amount, $amount_org, $type, $account_type = 1, $product_type = 'line')
    {
        $data = array(
            'order_id' => $order_id,
            'uid' => $uid,
            'amount' => $amount,
            'amount_carry' => $amount_org,
            'date' => date('Y-m-d H:i:s', time()),
            'type' => $type,
            'account_type' => $account_type,
            'product_type' => $product_type,
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    /**
     * 生成提现记录
     */
    public function insertWithdraw($withdraw, $amountCarry, $product_type = 'withdraw')
    {
        $data = array(
            'order_id' => $withdraw->withdraw_id,
            'uid' => $withdraw->uid,
            'amount' => $withdraw->amount,
            'amount_carry' => $amountCarry,
            'date' => date('Y-m-d H:i:s', time()),
            'type' => 2,                    //2提现
            'account_type' => 1,            //1可提现
            'product_type' => $product_type  //提现记录
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
}